window.Alert = {
    _alert: null,           // prefab
    _detailLabel:   null,   // 内容
    _cancelButton:  null,   // 取消按钮
    _cancelLabel:  null,   // 取消文本

    _enterButton:   null,   // 确定按钮
    _enterLabel:  null,   // 确定文本

    _enterCallBack: null,   // 回调事件
    _cancelCallBack: null,   // 回调事件
    _animSpeed:     0.0,    // 动画速度
    _isShow: false //是否展示
};






/**
 * detailString :   内容 string 类型.
 * enterCallBack:   确定点击事件回调  function 类型.
 * neeCancel:       是否展示取消按钮 bool 类型 default YES.
 * duration:        动画速度 default = 0.3.
*/
Alert.show = function (detailString, cancelString,enterString,enterCallBack,needCancel, animSpeed) {
    cc.log("Alert show needCancel ="+needCancel);
    // 引用
    var self = this;

    // 判断
    // if (Alert._alert != null) {
    //     cc.log("已经创建了alert return");
    //     return;
    // }

    Alert._animSpeed = animSpeed ? animSpeed : Alert._animSpeed;
    // 加载 prefab 创建
    cc.loader.loadRes("/mzGameResource/prefab/Alert", cc.Prefab, function (error, prefab) {
        if (error) {
            cc.error(error);
            return;
        }

        // 实例 
        var alert = cc.instantiate(prefab);

        // Alert 持有
        Alert._alert = alert;

        // 动画 
        var cbFadeOut = cc.callFunc(self.onFadeOutFinish, self);
        var cbFadeIn = cc.callFunc(self.onFadeInFinish, self);
        self.actionFadeIn = cc.sequence(cc.spawn(cc.fadeTo(Alert._animSpeed, 255), cc.scaleTo(Alert._animSpeed, 1.0)), cbFadeIn);
        self.actionFadeOut = cc.sequence(cc.spawn(cc.fadeTo(Alert._animSpeed, 0), cc.scaleTo(Alert._animSpeed, 1.0)), cbFadeOut);

        // 获取子节点
        Alert._detailLabel = cc.find("alertBackground/detailLabel", alert).getComponent(cc.Label);
        Alert._cancelButton = cc.find("alertBackground/cancelButton", alert);
        Alert._cancelLabel = cc.find("alertBackground/cancelButton/cancel_label", alert).getComponent(cc.Label);
        Alert._enterButton = cc.find("alertBackground/enterButton", alert);
        Alert._enterLabel = cc.find("alertBackground/enterButton/enter_label", alert).getComponent(cc.Label);


        // 添加点击事件
        Alert._enterButton.on('click', self.onButtonClicked, self);
        Alert._cancelButton.on('click', self.onButtonClicked, self);

        // 父视图
        Alert._alert.parent = cc.find("Canvas");

        // 展现 alert
        self.startFadeIn();

        // 参数
        self.configAlert(detailString,cancelString,enterString, enterCallBack, needCancel, animSpeed);
        
    });

    // 参数
    self.configAlert = function (detailString, cancelString,enterString,enterCallBack, needCancel, animSpeed) {
        // 回调
        Alert._enterCallBack = enterCallBack;

        // 内容
        Alert._detailLabel.string = detailString;
        Alert._enterLabel.string = enterString;
        Alert._cancelLabel.string = cancelString;

        // 是否需要取消按钮
        if (needCancel || needCancel == undefined) { // 显示
            Alert._cancelButton.active = true;
        } else {  // 隐藏
            Alert._cancelButton.active = false;
            Alert._enterButton.x = 0;
        }
    };

    // 执行弹进动画
    self.startFadeIn = function () {
        cc.log("Alert startFadeIn Alert._alert ="+Alert._alert);
        if(Alert._isShow) {
            cc.log("Alert 已经显示了，不再显示");
           return; 
        }

        Alert._isShow = true;
        cc.eventManager.pauseTarget(Alert._alert, true);
        Alert._alert.position = cc.p(0, 0);
        Alert._alert.setScale(1);
        Alert._alert.opacity = 0;
        Alert._alert.runAction(self.actionFadeIn);
    };

    // 执行弹出动
    self.startFadeOut = function () {
        cc.log("Alert startFadeOut Alert._alert ="+Alert._alert);
        if(!Alert._isShow) {
           cc.log("Alert 已经销毁了，不再销毁");
           return; 
        }
        Alert._isShow = false;
        cc.eventManager.pauseTarget(Alert._alert, true);
        Alert._alert.runAction(self.actionFadeOut);
    };

    // 弹进动画完成回调
    self.onFadeInFinish = function () {
        cc.log("onFadeInFinish");
        cc.eventManager.resumeTarget(Alert._alert, true);
    };

    // 弹出动画完成回调
    self.onFadeOutFinish = function () {
        cc.log("onFadeOutFinish");
        self.onDestory();
    };

    // 按钮点击事件
    self.onButtonClicked = function(event){
        if(event.target.name == "enterButton"){
            if(self._enterCallBack){
                self._enterCallBack();
            }
        }
        self.startFadeOut();
    };

    // 销毁 alert (内存管理还没搞懂，暂且这样写吧~v~)
    self.onDestory = function () {
        Alert._alert.destroy();
        Alert._enterCallBack = null;
        Alert._alert = null;
        Alert._detailLabel = null;
        Alert._cancelButton = null;
        Alert._enterButton = null;
        Alert._animSpeed = 0.0;
        cc.log("已经销毁了alert");
    };
};


//展示带倒计时的dialog
Alert.showCountDownDialog = function (detailString, cancelString,enterString,enterCallBack, cancelCallBack,needCancel, animSpeed,needCountDown,countDownTime,node) {

    // 引用
    var self = this;

    // 判断
    if (Alert._alert != null) {
        cc.log("已经创建了alert return");
        return;
    }

    // 
    Alert._animSpeed = animSpeed ? animSpeed : Alert._animSpeed;

    // 加载 prefab 创建
    cc.loader.loadRes("/mzGameResource/prefab/Alert", cc.Prefab, function (error, prefab) {

        if (error) {
            cc.error(error);
            return;
        }

        // 实例 
        var alert = cc.instantiate(prefab);

        // Alert 持有
        Alert._alert = alert;

        // 动画 
        var cbFadeOut = cc.callFunc(self.onFadeOutFinish, self);
        var cbFadeIn = cc.callFunc(self.onFadeInFinish, self);
        self.actionFadeIn = cc.sequence(cc.spawn(cc.fadeTo(Alert._animSpeed, 255), cc.scaleTo(Alert._animSpeed, 1.0)), cbFadeIn);
        self.actionFadeOut = cc.sequence(cc.spawn(cc.fadeTo(Alert._animSpeed, 0), cc.scaleTo(Alert._animSpeed, 1.0)), cbFadeOut);

        // 获取子节点
        Alert._detailLabel = cc.find("alertBackground/detailLabel", alert).getComponent(cc.Label);
        Alert._cancelButton = cc.find("alertBackground/cancelButton", alert);
        Alert._cancelLabel = cc.find("alertBackground/cancelButton/cancel_label", alert).getComponent(cc.Label);
        Alert._enterButton = cc.find("alertBackground/enterButton", alert);
        Alert._enterLabel = cc.find("alertBackground/enterButton/enter_label", alert).getComponent(cc.Label);


        // 添加点击事件
        Alert._enterButton.on('click', self.onButtonClicked, self);
        Alert._cancelButton.on('click', self.onButtonClicked, self);

        // 父视图
        Alert._alert.parent = cc.find("Canvas");

        // 展现 alert
        self.startFadeIn();

        // 参数
        self.configAlert(detailString,cancelString,enterString, enterCallBack, cancelCallBack, needCancel, animSpeed,needCountDown,countDownTime,node);
        
    });

    // 参数
    self.configAlert = function (detailString, cancelString,enterString,enterCallBack,cancelCallBack, needCancel, animSpeed,needCountDown,countDownTime,node) {

        // 回调
        Alert._enterCallBack = enterCallBack;
        Alert._cancelCallBack = cancelCallBack;


        // 内容
        Alert._detailLabel.string = detailString;
        Alert._enterLabel.string = enterString;
        Alert._cancelLabel.string = cancelString;

        // 是否需要取消按钮
        if (needCancel || needCancel == undefined) { // 显示
            Alert._cancelButton.active = true;
        } else {  // 隐藏
            Alert._cancelButton.active = false;
            Alert._enterButton.x = 0;
        }

        if(needCountDown) {
            self.countDownTimeCallBack =  function() {
                countDownTime--;
                Alert._enterLabel.string = enterString + " ("+countDownTime+")";
                if(countDownTime <= 0) {
                    if(self._enterCallBack){
                        self._enterCallBack();
                    }
                    self.startFadeOut();
                    node.unschedule(self.countDownTimeCallBack);
                }
            }.bind(this);
            Alert._enterLabel.string = enterString + " ("+countDownTime+")";
            node.schedule(self.countDownTimeCallBack, 1);
        }
    };

    // 执行弹进动画
    self.startFadeIn = function () {
        if(Alert._alert) {
            cc.eventManager.pauseTarget(Alert._alert, true);
            Alert._alert.position = cc.p(0, 0);
            Alert._alert.setScale(1);
            Alert._alert.opacity = 0;
            Alert._alert.runAction(self.actionFadeIn);
        }
    };

    // 执行弹出动画
    self.startFadeOut = function () {
        if(Alert._alert) {
            cc.eventManager.pauseTarget(Alert._alert, true);
            Alert._alert.runAction(self.actionFadeOut);
        }
    };

    // 弹进动画完成回调
    self.onFadeInFinish = function () {
        cc.eventManager.resumeTarget(Alert._alert, true);
    };

    // 弹出动画完成回调
    self.onFadeOutFinish = function () {
        self.onDestory();
    };

    // 按钮点击事件
    self.onButtonClicked = function(event){
        if(event.target.name == "enterButton"){
            if(self._enterCallBack){
                self._enterCallBack();
            }
        }else if(event.target.name == "cancelButton") {
            if(self._cancelCallBack){
                self._cancelCallBack();
            }
        }
        self.startFadeOut();
    };

    // 销毁 alert (内存管理还没搞懂，暂且这样写吧~v~)
    self.onDestory = function () {
        Alert._alert.destroy();
        Alert._enterCallBack = null;
        Alert._alert = null;
        Alert._detailLabel = null;
        Alert._cancelButton = null;
        Alert._enterButton = null;
        Alert._animSpeed = 0.0;
        cc.log("已经销毁了alert");
    };
};
